﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Home.Haushalt.Lib;
using Home.Haushalt.Lib.Element;
using Infragistics.Win.UltraWinListView;
using Infragistics.Win.UltraWinGrid;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmPropertyPosten : FrmProperty
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmPropertyPosten()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        public FrmPropertyPosten(DataRow row)
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmPropertyPosten_Load(object sender, EventArgs e)
        {
            ElementObject.OnAcceptChanges += new EventHandler<HEventArgs>(_elementObject_OnAcceptChanges);

            pnlTurnus.Enabled = true;// DAO.GetPropertyAtId(Postengroup.ElementType, ElementObject.Row[Posten.Fields.PostengruppeId].ToInt(), Postengroup.Fields.Turnus).ToBool(false);

            int serieId = ElementObject.Row[Posten.Fields.SerieId].ToInt();
            serieId = (serieId > 0) ? serieId : -1;

            int postenId = ElementObject.Row[Posten.Fields.Id].ToInt();
            postenId = (postenId > 0) ? postenId : -1;

            //Tab Parentposten
            this.AddTab(Posten.ElementType, Posten.Fields.SerieId, serieId);

            //Tab Documenten
            this.AddTab(Document.ElementType, Document.Fields.PostenId, postenId, serieId);

            //Tab Buchungen
            this.AddTab(Date.ElementType, Date.Fields.PostenId, postenId, serieId);

            //Tab Members
            this.AddTab(Member.ElementType, Member.Fields.PostenId, postenId, serieId);

            //Binding
            AddBinding(txtName.DataBindings, "Text", Posten.Fields.Name);
            AddBinding(txtSum.DataBindings, "Text", Posten.Fields.Summe);

            AddBinding(lstTurnus.DataBindings, "CheckedKeys", Posten.Fields.Turnus);

            AddBinding(cbBasement.DataBindings, "Checked", Posten.Fields.Basement);
            AddBinding(cbOwn.DataBindings, "Checked", Posten.Fields.OwnId);

            lstPostengroup.AddBinding(_elementObject, Posten.Fields.PostengruppeId);
            lstOpponent1.AddBinding(_elementObject, Posten.Fields.OpponentId);
            lstOpponent2.AddBinding(_elementObject, Posten.Fields.OpponentExt1Id);
            lstOpponent3.AddBinding(_elementObject, Posten.Fields.OpponentExt2Id);
            lstPosten.AddBinding(_elementObject, Posten.Fields.ParentPostenId);
            lstSaldotype.AddBinding(_elementObject, Posten.Fields.SaldotypeId);
            lstGroup.AddBinding(_elementObject, Posten.Fields.GroupId);
            lstDay.AddBinding(_elementObject, Posten.Fields.DateDay);
            lstTurnusPeriode.AddBinding(_elementObject, Posten.Fields.TurnusPeriode);

            AddBinding(calFrom.DataBindings, "Value", Posten.Fields.DateFrom);
            AddBinding(calTo.DataBindings, "Value", Posten.Fields.DateTo);

            DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _elementObject_OnAcceptChanges(object sender, HEventArgs e)
        {
            WindowController.RefreshMonthbalanceGrids();

            SetAppState();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckAll_Click(object sender, EventArgs e)
        {
            lstTurnus.Items.Cast<UltraListViewItem>().ToList().ForEach(item => item.CheckState = CheckState.Checked);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckNone_Click(object sender, EventArgs e)
        {
            lstTurnus.Items.Cast<UltraListViewItem>().ToList().ForEach(item => item.CheckState = CheckState.Unchecked);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstTurnusPeriode_RowSelected(object sender, RowSelectedEventArgs e)
        {
            int turnusPeriode=lstTurnusPeriode.SelectedRow.Cells[Turnus.Fields.ID].Value.ToInt();
            lstTurnus.SetDataSource(lstTurnusPeriode.SelectedRow.Cells[Turnus.Fields.ID].Value.ToInt().Equals(0) ? DAO.Monthnames : DAO.Weekdaynames);
            lstDay.Visible = lblDay.Visible = turnusPeriode.Equals(0);

            lstTurnus.Height = turnusPeriode.Equals(0) ? 57 : 45;
        }

        private void lstTurnusPeriode_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {

        }



    }
}
